package logic

import (
	"context"
	"user_rpc/models"

	"user_rpc/internal/svc"
	"user_rpc/pb/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateUserLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
	return &UpdateUserLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateUserLogic) UpdateUser(in *user.UserInfo) (*user.UserInfo, error) {
	err := l.svcCtx.UserModel.Update(l.ctx, &models.User{Id: in.Id, Name: in.Name, Password: in.Password})
	if err != nil {
		l.Logger.Errorf("UpdateUser failed: id=%d, name=%s, pass=%s, error: %v", in.Id, in.Name, in.Password, err)
		return nil, err
	}
	return &user.UserInfo{Id: in.Id, Name: in.Name, Password: in.Password}, nil
}
